//
//  BecomeVipView.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/27.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "BecomeVipView.h"
#import "MemberCell.h"
#import "MBProgressHUD+MJ.h"

@interface BecomeVipView()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHCons;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomInsetCons;
@property (nonatomic,assign) float anniH;
@property (nonatomic,strong) MemberModel *selectModel;

@end

@implementation BecomeVipView
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberCell class])];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}
- (void)setMembers:(NSArray *)members
{
    _members = members;
    self.anniH = 147+members.count*50+30;
    self.bottomViewHCons.constant = self.anniH;
    self.bottomInsetCons.constant = -self.anniH;
    [self layoutIfNeeded];
}
- (IBAction)disClick:(UIButton *)btn
{
    [self animatedHiden];
}

- (IBAction)btnClick:(UIButton *)btn
{
    if (!self.selectModel) {
        [MBProgressHUD showOnlyText:@"请选择要开通的合伙人" view:nil];
        return;
    }
    if (self.becomeBlock) {
        self.becomeBlock(self.selectModel);
    }
    [self animatedHiden];
}
- (void)animatedShow
{
    self.bottomInsetCons.constant = 0;
    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)animatedHiden
{
    self.bottomInsetCons.constant = -self.anniH;
    [UIView animateWithDuration:0.4 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.selectModel.isSelect = NO;
        [self removeFromSuperview];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self animatedHiden];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.members.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberCell class])];
    cell.model = self.members[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectBlock = ^(MemberModel *model) {
        self.selectModel.isSelect = NO;
        self.selectModel = model;
        [self.tableView reloadData];
    };
    return cell;
}


@end
